package controllers

import (
	"beego/models"
	"beego/redis"
	"github.com/astaxie/beego"
	"time"
)

type GetGameRankController struct {
	beego.Controller
}
type AddGameScoreController struct {
	beego.Controller
}
type ResetGameScoreController struct {
	beego.Controller
}

var num = make(map[int64]bool)

func (c *AddGameScoreController) Get() {
	_, ok := num[time.Now().Unix()]
	userId := c.Ctx.Input.Param(":userId")
	gameId := c.Ctx.Input.Param(":gameId")
	redis.Init()
	if ok {
		redis.Zadd(0, userId, gameId)
	} else {
		redis.Zadd(1, userId, gameId)
	}
	res := models.GameJSON{redis.Zrevrank(gameId, userId),
		redis.Zscore(gameId, userId), userId, gameId}
	c.Data["json"] = res
	c.ServeJSON()
	num[time.Now().Unix()] = true

}

func (c *ResetGameScoreController) Get() {
	userId := c.Ctx.Input.Param(":userId")
	redis.Init()
	redis.Zadd(-redis.Zscore("3*3", userId), userId, "3*3")
	redis.Zadd(-redis.Zscore("4*4", userId), userId, "4*4")
	redis.Zadd(-redis.Zscore("5*5", userId), userId, "5*5")
	c.Data["json"] = 1
	c.ServeJSON()
}

func (c *GetGameRankController) Get() {
	userId := c.Ctx.Input.Param(":userId")
	gameId := c.Ctx.Input.Param(":gameId")
	redis.Init()
	res := models.GameJSON{redis.Zrevrank(gameId, userId),
		redis.Zscore(gameId, userId), userId, gameId}
	c.Data["json"] = res
	c.ServeJSON()
}
